import Vue from 'vue'
import VueRouter from 'vue-router'
import LoginView from '../views/LoginView.vue'
import store from '../store/index'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'login',
    component: LoginView
  },
  {
    path: '/home',
    name: 'home',
    children: [
      {
        path: "test",
        name: "test01",
        component: () => import('../views/Test01.vue')
      }, 
      {
        path: "test02/:button",
        name: "test02",
        component: () => import('../views/Test02.vue')
      },
      {
        path: "home",
        name: "homeson",
        component: () => import('../views/homeview/Home.vue')
      }, 
      {
        path: "self",
        name: "self",
        component: () => import('../views/homeview/Self.vue')
      }, 
      {
        path: "classinfo",
        name: "classinfo",
        component: () => import('../views/homeview/ClassInfoView.vue')
      }, 
      {
        path: "studentinfo",
        name: "studentinfo",
        component: () => import('../views/homeview/StudentInfo.vue')
      }, 
      {
        path: "taskinfo",
        name: "taskinfo",
        component: () => import('../views/homeview/TaskInfo.vue')
      }, 
      {
        path: "resources",
        name: "resources",
        component: () => import('../views/homeview/ResourceView.vue')
      }, 
      {
        path: "discuss",
        name: "discuss",
        component: () => import('../views/homeview/DiscussManageView.vue')
      }, 
      {
        path: "daily",
        name: "daily",
        component: () => import('../views/homeview/DailyView.vue')
      }, 
      {
        path: "video",
        name: "video",
        component: () => import('../views/homeview/VideoView.vue')
      }, 
      {
        path: "studentTask",
        name: "studentTask",
        component: () => import('../views/students/TaskInfo.vue')
      },
      {
        path: "studentResource",
        name: "studentResource",
        component: () => import('../views/students/ResourcesInfo.vue')
      },
      {
        path: "studentDiscuss",
        name: "studentDiscuss",
        component: () => import('../views/students/DiscussManageView.vue')
      },
      {
        path: "studentDaily",
        name: "studentDaily",
        component: () => import('../views/students/DailyInfo.vue')
      },
      {
        path: "studentVideo",
        name: "studentVideo",
        component: () => import('../views/students/VideoInfo.vue')
      },
      
    ],
    component: () => import('../views/HomeView.vue')
  },
  {
    path: '/about',
    name: 'about',
    component: () => import('../views/AboutView.vue')
  },
]

const router = new VueRouter({
  routes
})

//配置路由守卫
router.beforeEach((to, from, next) => {
  console.log(to)//去哪个页面
  console.log(from)//从哪个页面来
  if (to.name != "login") {
    //验证用户是否登录
    if (store.state.token != "") {
      //当前用户已经登录 放行
      next();
    } else {
      //到登录页
      next("/");
    }
  } else {
    next();
  }
})

export default router
